package study.config.apollo.support.bean;

import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Data
@Configuration("propBeanV2")
@ConfigurationProperties(prefix = "prop-v2")
@RefreshScope
public class Bean2 {

    private String name;

    private Integer age;

    @Autowired
    private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope;

    @ApolloConfigChangeListener(value = "config.yml", interestedKeyPrefixes = "prop-v2.")
    public void onChange(ConfigChangeEvent changeEvent) {
        log.info("Apollo配置发生改变");
        refreshScope.refresh("propBeanV2");
    }
}
